<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-23 09:38:54
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\BaseData;

use App\Repositories\CompanyRepository;
use App\Repositories\CompanyLinkManRepository;
use App\Http\Services\Service;
use App\Repositories\CompanyBilltradeRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property CompanyRepository $repository
 * @property CompanyLinkManRepository $link_man
 * @property CompanyBilltradeRepository $billtrade
 */
class CompanyService extends Service
    {
    public function __construct(CompanyRepository $repository, CompanyLinkManRepository $link_man, CompanyBilltradeRepository $billtrade)
        {
        $this->repository = $repository;
        $this->link_man   = $link_man;
        $this->billtrade  = $billtrade;
        }

    public function list()
        {
        // DB::enableQueryLog();
        $data = $this->repository->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }
    public function customerlist()
        {
        // DB::enableQueryLog();
        $data = $this->repository->whereStatus_id(0)->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }
    public function companylist()
        {
        // DB::enableQueryLog();
        $data = $this->repository->whereStatus_id(0)->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }
    public function factorylist()
        {
        // DB::enableQueryLog();
        $data = $this->repository->whereStatus_id(0)->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }

    protected function save($request)
        {
        // DB::enableQueryLog();
        $data = $request->toArray();
        // dd(DB::getQueryLog());
        // ACPrefix
        if ($data['category_id'] == 109) {
            $prefix = "A";
            }
        else if ($data['category_id'] == 108) {
            $prefix = "B";
            }
        else
            $prefix = "C";
        $data             = $this->repository->setPrefix($prefix)->autosave($data, false, ['link_mans', 'billtrade']);
        $data->finance_id = $data->category->finance_id;
        return $data->save();
        }
    protected function delete($request)
        {
        return $this->repository->delete($this->getRequestIds());
        }
    protected function deptcust($enable)
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($item) use ($enable)
            {
            $item->deptcust_id = $enable;
            return $item->save();
            });
        }
    protected function linkmanAdd()
        {
        return $this->link_man->autosave(request());
        }
    protected function billtradeAdd()
        {
        return $this->billtrade->autosave(request());
        }
    public function billtradeGet()
        {
        return $this->billtrade->condition()->get();
        }
    public function linkmanGet()
        {
        return $this->link_man->condition()->get();
        }

    }